$(function(){

	var cat1List = $('#cat1_list');
	var cat2List = $('#cat2_list');
	var cat3List = $('#cat3_list');
	var cat1Value = cat1List.find('input[type=hidden]');
	var cat2Value = cat2List.find('input[type=hidden]');
	var cat3Value = cat3List.find('input[type=hidden]');


	//显示一级分类
	var cat1Links = ["<a href='#' id='-1'>不限</a>"]
	$(cats).each(function(){
		cat1Links.push("<a href='#' id="+this['item_category']['id']+">"+this['item_category']['view_name']+"</a>");
	});
	cat1List.append(cat1Links.join(" "));


	//如果有cat1参数
	if(dftCat1){
		cat1List.find('a#'+dftCat1).addClass("highlight");
		cat1Value.val(dftCat1);
		if(dftCat1 != -1){
			var cat2Links = []
			$(cats).each(function(){
				if(this['item_category']['id'] == dftCat1)
				{
					$(this['item_category']['children']).each(function(){
						cat2Links.push("<a href='#' id="+this['id']+">"+this['view_name']+"</a>");
					});
					cat2List.append(cat2Links.join(" "));
				}
			});
		}

	}
	//如果有cat2参数
	if(dftCat2){
		cat2List.find('a#'+dftCat2).addClass("highlight");
		cat2Value.val(dftCat2);
		var cat3Links = []
		$(cats).each(function(){
			$(this['item_category']['children']).each(function(){
				if(this['id'] == dftCat2)
				{
					$(this['children']).each(function(){
						cat3Links.push("<a href='#' id="+this['id']+">"+this['view_name']+"</a>");
					});
					cat3List.append(cat3Links.join(" "));

				}
			});
		});
	}
	//如果有cat3参数
	if(dftCat3){
		cat3List.find('a#'+dftCat3).addClass("highlight");
		cat3Value.val(dftCat3);
	}
	//一级分类点击刷新
	cat1List.find('a').live('click',function(){
		if($(this).attr('id') === '-1'){
			cat1Value.val('');
		}else{
			cat1Value.val($(this).attr('id'));
		}
		cat2Value.val('');
		cat3Value.val('');
		cat2Links = [];
		cat1List.find('a').removeClass('highlight');
		$(this).addClass('highlight');
		var cat1Select = $(this).attr('id');
		if(cat1Select === '-1'){
			cat2List.find('a').remove();
			cat3List.find('a').remove();
		}
		else{
			$(cats).each(function(){
				if(this['item_category']['id'] == cat1Select)
				{
					$(this['item_category']['children']).each(function(){
						cat2Links.push("<a href='#' id="+this['id']+">"+this['view_name']+"</a>");
					});
					cat2List.find('a').remove();
					cat3List.find('a').remove();
					cat2List.append(cat2Links.join(" "));
				}
			});
		}

	});
	//二级分类点击刷新
	cat2List.find('a').live('click',function(){
		cat2Value.val($(this).attr('id'));
		cat3Value.val('');
		cat3Links = [];
		cat2List.find('a').removeClass('highlight');
		$(this).addClass('highlight');
		var cat2Select = $(this).attr('id');
		$(cats).each(function(){
			$(this['item_category']['children']).each(function(){
				if(this['id'] == cat2Select){
					$(this['children']).each(function(){
						cat3Links.push("<a href='#' id="+this['id']+">"+this['view_name']+"</a>");
					});
				}
			});
		});
		cat3List.find('a').remove();
		cat3List.append(cat3Links.join(" "));
	});
	//三级分类选定
	cat3List.find('a').live('click',function(){
		cat3Value.val($(this).attr('id'));
		cat3List.find('a').removeClass('highlight');
		$(this).addClass('highlight');
	});
	
	$("#ret_table th a").live('click',function() {
		$("#sort").val($(this).attr("rel"));
		$("form:first").submit();
	});
});
